/****************************************************************************
 * Copyright (C) 2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * SaveWindow.cpp
 *
 * for SaveGame Manager GX 2013
 ***************************************************************************/
#include "SaveWindow.hpp"
#include "../../Themes/CTheme.h"
#include "../../Controls/Application.h"

SaveWindow::SaveWindow()
	: ManageWindow()
{
	bIsEmuSave = (Settings.LoadedBrowser == ESAVE_BROWSER);
	
	dlWindow = NULL;
	buttons = NULL;
	ebuttons = NULL;
	
	wifiBoxImg = new GuiImage(btnBoxData);
	wifiBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	wifiBoxImg->SetPosition(-60, 6);
	
	btnWifi = new PictureButton("button_wifi.png", "button_wifi_over.png", btnSoundClick, btnSoundOver);
	btnWifi->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnWifi->SetPosition(-63, 8);
	btnWifi->SetTrigger(&trigA);
	btnWifi->Clicked.connect(this, &SaveWindow::OnWifiButtonClick);
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_WIISAVE_DOWNLOAD))
	{
		Append(wifiBoxImg);
		Append(btnWifi);
	}
	
	warningTxt = new GuiText(tr("WARNING: You need to have a save already installed in the Wii to install this one!!!"), 20, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	warningTxt->SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	warningTxt->SetPosition(-6, -24);
	warningTxt->SetMaxWidth(450, SCROLL_WIDE);
	
	saveInfos = new SaveInfos();
	Append(saveInfos);
	
	if(!bIsEmuSave)
	{
		buttons = new SaveButtons();
		buttons->BlockButtons.connect(this, &SaveWindow::OnBlockButtons);
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
			Append(buttons);
	}
	else
	{
		ebuttons = new ESaveButtons();
		ebuttons->BlockButtons.connect(this, &SaveWindow::OnBlockButtons);
		ebuttons->EntryDeleted.connect(this, &SaveWindow::OnEntryDeleted);
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
			Append(ebuttons);
	}
	
	SetInfos();
	
	Refresh.connect(this, &SaveWindow::OnRefresh);
	
	Application::Instance()->Append(this);
	Application::Instance()->SetUpdateOnly(this);
}

SaveWindow::~SaveWindow()
{
	RemoveAll();
	
	if(dlWindow)
		delete dlWindow;
	
	if(buttons)
		delete buttons;
	if(ebuttons)
		delete ebuttons;

	delete wifiBoxImg;
	
	delete btnWifi;
	
	delete warningTxt;
	
	delete saveInfos;
}

void SaveWindow::SetInfos()
{
	saveInfos->SetInfos(selIndex);
	
	Remove(warningTxt);
	if(bIsEmuSave)
	{
		ebuttons->SetActionButtons();
		
		if(CurrentBrowser()->IsNotInstalled(selIndex))
			Append(warningTxt);
	}
}

void SaveWindow::OnWifiButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	OnBlockButtons(true);
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
	{
		if(!bIsEmuSave)
		{
			buttons->SetEffect(EFFECT_FADE, -20);
			while(buttons->IsAnimated())
				Application::Instance()->updateEvents();
			Remove(buttons);
		}
		else
		{
			ebuttons->SetEffect(EFFECT_FADE, -20);
			while(ebuttons->IsAnimated())
				Application::Instance()->updateEvents();
			Remove(ebuttons);
		}
	}
	
	dlWindow = new DownloadSaveWindow(saveInfos->GetTid(), saveInfos->GetID(), this);
	dlWindow->End.connect(this, &SaveWindow::OnDownloadSaveEnd);
}

void SaveWindow::OnDownloadSaveEnd()
{
	delete dlWindow;
	dlWindow = NULL;
	
	if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
	{
		if(!bIsEmuSave)
		{
			buttons->SetEffect(EFFECT_FADE, 20);
			Append(buttons);
			while(buttons->IsAnimated())
				Application::Instance()->updateEvents();
		}
		else
		{
			ebuttons->SetEffect(EFFECT_FADE, 20);
			Append(ebuttons);
			while(ebuttons->IsAnimated())
				Application::Instance()->updateEvents();
		}
		
	}
	
	OnBlockButtons(false);
}

void SaveWindow::OnRefresh(bool inc)
{
	ManageWindow::OnRefresh(inc);
	
	if(!closing)
		SetInfos();
}

void SaveWindow::OnEntryDeleted()
{
	Explorer::Instance()->Reload();
	CurrentBrowser()->FilterList();
	
	selIndex--;
	POINT p = {0, 0};
	OnArrowButtonClick(btnRight, 0, p);
}

void SaveWindow::OnBlockButtons(bool block)
{
	int State = STATE_DEFAULT;
	if(block)
		State = STATE_DISABLED;
	
	btnWifi->SetState(State);
	
	if(bIsEmuSave)
	{
		if(!block && CurrentBrowser()->IsNotInstalled(selIndex))
			Append(warningTxt);
		else
			Remove(warningTxt);
	}
	
	ManageWindow::OnBlockButtons(block);
}
